<template>
	<view class="authorize">
		<view class="authorize-user">
			<open-data class="authorize-user__avatar" type="userAvatarUrl"></open-data>
			<open-data class="authorize-user__name" type="userNickName"></open-data>
		</view>
		<view class="authorize-title">
			登录/注册 车一转
		</view>
		<button class="authorize-button" type="primary" open-type="getUserInfo" @getuserinfo="handleGetUserInfo">
			<view class="authorize-button__icon iconfontPT iconweixin"></view>微信一键授权
		</button>
		<view class="authorize-pact">
			已阅读并同意：
			<navigator class="authorize-pact__link" url="/pages/used-car/xieyi?type=0">个人信息保护声明、</navigator>
			<navigator class="authorize-pact__link" url="/pages/used-car/xieyi?type=4">车一转个人隐私保护协议、</navigator>
			<navigator class="authorize-pact__link" url="/pages/used-car/xieyi?type=1">车一转共享平台服务使用协议、</navigator>
			<navigator class="authorize-pact__link" url="/pages/used-car/xieyi?type=3">车一转共享平台经销商使用服务协议</navigator>
		</view>
	</view>
</template>

<script>
import { login, getPhone } from '../apis/public/index.js';

export default {
	data() {
		return {
			code: '',
			session_key: '',
			openid: '',
			authorizePhone: '',
		};
	},
	onLoad(option) {
		console.log(this.$baseHost.qiniuHost)
		this.authorizePhone = +option.authorizePhone;
	},
	computed:{
		qiniuHost(){
			return this.$baseHost.qiniuHost
		}
	},
	methods: {
		
		handleTest(){
			this.seconds = 0
		},
		handleBack() {
			// uni.switchTab({
			// 	url: '/pages/index/index'
			// });
			// this.$store.dispatch('home-tab-bar/setCurrentTabbar', 0);
			uni.navigateBack()
		},
		handlePactPopupClose(){
			this.pactPopupShow = false
			this.$emit('popup-close')
		},
		startButtonTiming(){
			const timer = setInterval(()=>{
				this.seconds <= 0 && clearInterval(timer)
				this.seconds --
			},1000)
		},
		handleGetUserInfo(e) {

				const userInfo = e.mp.detail.userInfo;
				let _this = this;
				if (userInfo) {
					uni.login({
						success: res => {
							const params = {
								userInfo: {
									city: userInfo.city,
									gender: userInfo.gender,
									province: userInfo.province,
									nickName: userInfo.nickName,
									avatarUrl: userInfo.avatarUrl,
									factory_id: 1,
									phone: ''
								},
								code: res.code
							};
							let factoryid = uni.getStorageSync('factory_id');
							let phone = uni.getStorageSync('phone');
							if (factoryid) {
								params.userInfo.factory_id = factoryid;
							}
							if (phone) {
								params.userInfo.phone = phone;
							}
							login(params).then(response => {
								uni.setStorageSync('openId', response.data.openid);
								_this.$store.dispatch('user/setOpenId', response.data.openid);
								if (response.data.factory_id > 0) {
									uni.setStorageSync('factory_id', response.data.factory_id);
								}
								if (response.data.phone.length == 11) {
									uni.setStorageSync('phone', response.data.phone);
								}
								uni.setStorageSync('states', response.data.states); // 推广权限
								uni.setStorageSync('xn_factory_id', response.data.xn_factory_id); //虚拟厂家id
								uni.setStorageSync('distributor_id', response.data.distributor_id);
								uni.setStorageSync('userInfo', userInfo);
								uni.setStorageSync('userSig', response.data.im_token);
								uni.setStorageSync('userId', response.data.uid);
								uni.setStorageSync('userSigGroup', response.data.im_token_zuju);
								uni.setStorageSync('identity', response.data.identity);
								_this.$store.dispatch('user/setIdentity', +response.data.identity);
								uni.setStorageSync('userSigBoss', response.data.im_token_boss);
				
								const isMerchant = response.data.states.includes('20') && response.data.distributor_id > 0;
								uni.setStorageSync('isMerchant', isMerchant);
								if (!uni.getStorageSync('factory_id') && uni.getStorageSync('showSataus')) {
									uni.showModal({
										title: '温馨提示',
										content: '请从合作门店进入'
									});
									uni.removeStorageSync('openId');
									return;
								}
								uni.navigateBack({
									delta: 1
								});
							}).catch(e=>{
								uni.showModal({ title:'出错啦！',content:e.message,showCancel:false})
							});
						}
					});
				}
	
		}
	}
};
</script>

<style scoped lang="less">
.authorize{
	padding: 30rpx;
	padding-top: 15vh;
	box-sizing: border-box;
	width: 100vw;
	height: 100vh;
	display: flex;
	flex-direction: column;
	align-items: center;
}
.authorize-user{
	display: flex;
	flex-direction: column;
	align-items: center;
	
	&__avatar{
		width: 200rpx;
		height: 200rpx;
		border-radius: 50%;
		overflow: hidden;
		display: block;
		margin-bottom: 20rpx;
	}
	&__name{
		color: #666666;
	}
}
.authorize-title{
	display: flex;
	justify-content: center;
	font-size: 50rpx;
	margin: 50rpx 0;
}
.authorize-button{
	margin-top: 100rpx;
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	line-height: 1em;
	height: 88rpx;
	&__icon{
		color: #FFFFFF;
		font-size: 50rpx;
		margin-right: 10rpx;
	}
}
.authorize-pact{
	margin-top:50rpx;
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	justify-content: center;
	color: #999999;
	font-size: 24rpx;
	&__link{
		color: #666666;
	}
}
</style>
